package com.llf.Config;

import at.pollux.thymeleaf.shiro.dialect.ShiroDialect;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.LinkedHashMap;
import java.util.Map;

@Configuration
public class ShiroConfig {
    @Autowired
     private UserRealm userRealm;
    @Autowired
    private  DefaultWebSecurityManager SecurityManager;


    // ShiroFilterFactoryBean
    @Bean
    public ShiroFilterFactoryBean getshiroFilterFactoryBean(){
        ShiroFilterFactoryBean bean = new ShiroFilterFactoryBean();
        //设置安全管理器
        bean.setSecurityManager(SecurityManager);
        //设置过滤器
        /*
            anon：无需认证直接访问
            authc：必须认证才能访问
            user：必须拥有记住我功能才能访问
            perms：拥有对某个资源的权限才能访问
         */
        Map<String, String> FilterMap = new LinkedHashMap<>();
        //拦截条件设置
        FilterMap.put("/toAbout","anon");
        FilterMap.put("/toArchives","anon");
        FilterMap.put("/toBlog","anon");
        FilterMap.put("/toIndex","anon");
        FilterMap.put("/toTypes","anon");
        //用户 拥有authc权限 才能访问
        FilterMap.put("/toBlogs","authc");
        FilterMap.put("/toBlogAdd","authc");
        FilterMap.put("/admin/**","authc");
        FilterMap.put("/static/**","anon");

        //注销
        FilterMap.put("/toLogOut","logout");
        bean.setFilterChainDefinitionMap(FilterMap);
        //未授权页面
        bean.setUnauthorizedUrl("/Unauthorized");
        //跳转登录网站
        bean.setLoginUrl("/toLogin");

        return  bean;
    }

    //DefaultWebSecurityManager
    @Bean
    public DefaultWebSecurityManager  getdefaultWebSecurityManager(){
        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
        //关联realm

        securityManager.setRealm(userRealm);
        return securityManager;
    }

    //创建UesrRealm对象 需要自定义类
    @Bean
    public UserRealm userRealm(){
        return  new UserRealm();
    }

    //整合ShiroDialect

    @Bean
    public ShiroDialect getShiroDialect(){
        return  new ShiroDialect();
    }
}
